/**
 * Copyright 2009 Emory University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated  documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions: The 
 * above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.markgrand.structuredSql

/**
 * Append a REFERENCES column constraint to a StringBuilder.
 * 
 * @param constraintName
 *      An optional name for the column constraint.
 */
class ReferencesColumnConstraint protected (val constraintName:Option[String], 
                                            val refTable : TableName, 
                                            val refColumnName:Option[String], 
                                            val matchType:Option[ForeignKeyMatch.Value], 
                                            val updateAction:Option[ForeignKeyAction.Value], 
                                            val deleteAction:Option[ForeignKeyAction.Value]) extends ColumnConstraint with References {
  val refColumnNames = if (refColumnName.isDefined) List(refColumnName.get) else List[String]()
  
  def append(builder:StringBuilder) {
    if (constraintName.isDefined) {
      appendConstraintName(constraintName, builder)
      builder.append(" ")
    }
    appendReferences(builder)
  }
}

object ReferencesColumnConstraint {
  def apply(refTable:TableName) = 
    {new ReferencesColumnConstraint(None, refTable, None, None, None, None)}
  
  def apply(constraintName:String, refTable:TableName) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, None, None, None, None)}

  def apply(refTable:TableName, columnName:String) = 
    {new ReferencesColumnConstraint(None, refTable, Some(columnName), None, None, None)}
  
  def apply(constraintName:String, refTable:TableName, columnName:String) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, Some(columnName), None, None, None)}

  def apply(refTable:TableName, matchType:ForeignKeyMatch.Value) = 
    {new ReferencesColumnConstraint(None, refTable, None, Some(matchType), None, None)}
  
  def apply(constraintName:String, refTable:TableName, matchType:ForeignKeyMatch.Value) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, None, Some(matchType), None, None)}

  def apply(refTable:TableName, columnName:String, matchType:ForeignKeyMatch.Value) = 
    {new ReferencesColumnConstraint(None, refTable, Some(columnName), Some(matchType), None, None)}
  
  def apply(constraintName:String, refTable:TableName, columnName:String, matchType:ForeignKeyMatch.Value) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, Some(columnName), Some(matchType), None, None)}

  def apply(refTable:TableName, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(None, refTable, None, None, Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, None, None, Some(updateAction), Some(deleteAction))}

  def apply(refTable:TableName, columnName:String, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(None, refTable, Some(columnName), None, Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, columnName:String, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, Some(columnName), None, Some(updateAction), Some(deleteAction))}

  def apply(refTable:TableName, matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(None, refTable, None, Some(matchType), Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, None, Some(matchType), Some(updateAction), Some(deleteAction))}

  def apply(refTable:TableName, columnName:String, matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(None, refTable, Some(columnName), Some(matchType), Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:String, refTable:TableName, columnName:String, matchType:ForeignKeyMatch.Value, updateAction:ForeignKeyAction.Value, deleteAction:ForeignKeyAction.Value) = 
    {new ReferencesColumnConstraint(Some(constraintName), refTable, Some(columnName), Some(matchType), Some(updateAction), Some(deleteAction))}
  
  def apply(constraintName:Option[String], 
            refTable : TableName, 
            refColumnName:Option[String], 
            matchType:Option[ForeignKeyMatch.Value], 
            updateAction:Option[ForeignKeyAction.Value], 
            deleteAction:Option[ForeignKeyAction.Value]) = {
    new ReferencesColumnConstraint(constraintName, refTable, refColumnName, matchType, updateAction, deleteAction)
  }
}
